//
//  LivePlayURLTableViewController.m
//  LiveVideoCoreDemo
//
//  Created by 龚丁禧 on 16/4/27.
//  Copyright © 2016年 com.Alex. All rights reserved.
//

#import "LivePlayURLTableViewController.h"
#import "LivePlayURLTableViewCell.h"
#import "PlayHistoryWordsManager.h"
#import "LivePlayViewController.h"

static NSString* reuseIdentifier = @"reuseIdentifier";

@interface LivePlayURLTableViewController () <UITextViewDelegate> {
    NSArray* _historyWords;
    UIButton* _backButton;
    UITextView* _input;
    UIButton* _startButton;
    LivePlayViewController* _playViewController;
}

@end

@implementation LivePlayURLTableViewController

- (void)UIInit {
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat fScreenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat fScreenH = [UIScreen mainScreen].bounds.size.height;
    
    _backButton = [[UIButton alloc] initWithFrame: CGRectMake(10, 30, 50, 40)];
    _backButton.backgroundColor = [UIColor blueColor];
    [_backButton setTitle: @"返回" forState: UIControlStateNormal];
    _backButton.tintColor = [UIColor whiteColor];
    _backButton.layer.masksToBounds = YES;
    _backButton.layer.cornerRadius = 5.0;
    [_backButton addTarget: self action:@selector(back) forControlEvents: UIControlEventTouchDown];
    [self.view addSubview: _backButton];
    
    _startButton = [[UIButton alloc] initWithFrame: CGRectMake(fScreenW - 50 - 10, 30, 50, 40)];
    _startButton.backgroundColor = [UIColor blueColor];
    [_startButton setTitle: @"开始" forState: UIControlStateNormal];
    _startButton.tintColor = [UIColor whiteColor];
    _startButton.layer.masksToBounds = YES;
    _startButton.layer.cornerRadius = 5.0;
    [_startButton addTarget: self action:@selector(play) forControlEvents: UIControlEventTouchDown];
    [self.view addSubview: _startButton];
    
    _input = [[UITextView alloc] initWithFrame: CGRectMake(70, 30, fScreenW - 70 - 70, 40)];
    _input.backgroundColor = [UIColor blueColor];
    _input.tintColor = [UIColor whiteColor];
    [_input setFont: [UIFont fontWithName:@"Arial" size:18]];
    _input.textColor = [UIColor whiteColor];
    
    _input.delegate = self;
    
    [self.view addSubview:_input];
    
    self.tableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 70, fScreenW, fScreenH - 70)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview: self.tableView];
    [self.tableView registerClass: [LivePlayURLTableViewCell class] forCellReuseIdentifier:reuseIdentifier];
}

- (void)back {
    [self dismissViewControllerAnimated: YES completion:^{
        
    }];
}

- (void)play {
    [_input resignFirstResponder];
    if (_input.text.length == 0) {
        return;
    }
    
    [[PlayHistoryWordsManager shareInstance] addSearchHistoryWord: _input.text];
    
    _playViewController = [[LivePlayViewController alloc] init];
    _playViewController.playUrl = _input.text;
    [self presentViewController: _playViewController animated: YES completion:^{
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self UIInit];
}

- (void)viewDidAppear:(BOOL)animated {
    
    NSMutableArray *words = [[NSMutableArray alloc] initWithArray:[[PlayHistoryWordsManager shareInstance] searchHistoryWords]];
    [words addObject:@"rtmp://test.ws.pull.live.slw117.top/live/ios12323"];
    [words addObject:@"rtmp://live.hkstv.hk.lxdns.com/live/hks"];
    [words addObject:@"rtmp://rtmp.yunlive.cdn.pingan.com.cn/yunlive/BXlYmS296E"];
    _historyWords = words;
    
    [self.tableView reloadData];
    
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_historyWords count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LivePlayURLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: reuseIdentifier forIndexPath:indexPath];
    
    cell.url = [_historyWords objectAtIndex: indexPath.row];
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _input.text = [_historyWords objectAtIndex: indexPath.row];
    
    [self.tableView deselectRowAtIndexPath: indexPath animated: YES];
}

#pragma mark - UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }

    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    [textView resignFirstResponder];
    return YES;
}

@end
